package com.ywjh.inter.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

public class HTTPHelper {
	public static String deleteData(String getUrl, Map<String,String> header) throws IOException {
		String result = "";
		HttpURLConnection connection = null;
		try {
			// 创建连接
			URL url = new URL(getUrl);
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("DELETE");
			connection.setRequestProperty("Content-Type", "application/json");
			connection.setRequestProperty("Accept", "application/json");
			if(header!=null) {
				for(String key:header.keySet()) {
					connection.setRequestProperty(key, header.get(key));
				}
			}
			connection.connect();

			// 读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				sb.append(lines);
			}
			result = sb.toString();

			reader.close();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}

		return result;
	}
	
	
	/**
	 * @param h 
	 * @param args
	 * @throws IOException 
	 */
	public static String getData(String getUrl, Map<String,String> header) throws IOException {
		String result = "";
		HttpURLConnection connection = null;
		try {
			// 创建连接
			URL url = new URL(getUrl);
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("GET");
			connection.setRequestProperty("Content-Type", "application/json");
			connection.setRequestProperty("Accept", "application/json");
			if(header!=null) {
				for(String key:header.keySet()) {
					connection.setRequestProperty(key, header.get(key));
				}
			}
			connection.connect();

			// 读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				sb.append(lines);
			}
			result = sb.toString();

			reader.close();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			// 断开连接
			if (connection != null) {
				connection.disconnect();
			}
		}

		return result;
	}
	
	/**
	 * @param h 
	 * @param args
	 * @throws IOException 
	 */
	public static String postData(String postUrl, JSONObject postData, Map<String,String> header) throws IOException {
		String result = "";
		HttpURLConnection connection = null;
		try {
			// 创建连接
			URL url = new URL(postUrl);
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Type", "application/json");
			connection.setRequestProperty("Accept", "application/json");
			if(header!=null) {
				for(String key:header.keySet()) {
					connection.setRequestProperty(key, header.get(key));
				}
			}
			connection.connect();
			// POST请求
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			out.writeBytes(postData.toString());
			out.flush();
			out.close();

			// 读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				sb.append(lines);
			}
			result = sb.toString();

			reader.close();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			// 断开连接
			if (connection != null) {
				connection.disconnect();
			}
		}

		return result;
	}


	public static String getIP(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		//使用动态域名加速，会导致ip为多个，分开的ip地址
		if(ip!=null&&ip.indexOf(",")>-1) {
			ip=ip.split(",")[0];
		}
		return ip;
	}
}
